import scala.util.{Failure, Success, Using}

object UsingTest {

  def main(args: Array[String]): Unit = {
    Using(new ExceptionalResource()) { exp =>
      exp.processSomething()
    } match {
      case Success(_) => println("success")
      case Failure(exception) => exception.printStackTrace()
    }
  }

  class ExceptionalResource extends AutoCloseable {

    def processSomething(): Unit = {
      throw new IllegalArgumentException("Thrown from processSomething()")
    }

    override def close(): Unit = throw new NullPointerException("Thrown from close()")
  }

}
